﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;

namespace ctem.gui.SignalChart
{
    public class Series
    {
        public Chart owner = null;
        // значения сигнала
        //private List<float> m_items = new List<float>();
        private List<PointF> m_items = new List<PointF>();
        public int CountOfPoints { get { return m_items.Count; } }
        internal string Name;

        internal Color ColorOfPoints;

        public Series(string _name, Color _color)
        {
            Name = _name;
            ColorOfPoints = _color;
        }

        public PointF GetItem(int index)
        {
            if ((index >= 0) && (index < m_items.Count)) return m_items[index]; else return new PointF();
        }

        internal void Clear()
        {
            m_items.Clear();
        }

        internal void AddItem(PointF p)
        {
            m_items.Add(p);
        }

        internal void SavePoints(string file_name)
        {

        }


        internal void Draw(RectangleF ViewRect,int canvas_w, int canvas_h, Graphics g, bool IsLog)
        {
            // кисть, которой производится отрисовка
            SolidBrush br = new SolidBrush(ColorOfPoints);
            // для плавной отрисовки использую 2 точки
            PointF[] points = new PointF[2];

            int index1 = 0, index2 = CountOfPoints;
            for (int i = 0; i < CountOfPoints; i++)
            {
                if (m_items[i].X < ViewRect.Left) index1 = i;
                if (m_items[i].X > (ViewRect.Left + ViewRect.Width))
                {
                    index2 = i;
                    break;
                }
            }

            for (int index = index1; index <= index2; index++)
            {
                if ((index >= 0) && (index < m_items.Count - 1))
                {
                    //!!! Вот здесь.. ДУРА!!!!
                    // if ((m_items[index+1].X - m_items[index].X) == 1)
                    if (((m_items[index].Y  - ViewRect.Top) < 1000000) && ((m_items[index].Y - ViewRect.Top) > -1000000) && ((m_items[index + 1].Y - ViewRect.Top) > -1000000) && ((m_items[index + 1].Y - ViewRect.Top) < 1000000))
                    {
                        //points[0] = new PointF((float)(m_items[index].X * ZoomX - ViewRect.Left * ZoomX), (float)(owner.ConvertY((float)(m_items[index].Y * ZoomY - ViewRect.Top))));
                        //points[1] = new PointF((float)(m_items[index + 1].X * ZoomX - ViewRect.Left * ZoomX), (float)(owner.ConvertY((float)(m_items[index + 1].Y * ZoomY - ViewRect.Top))));
                        float val1_X, val1_Y, val2_X, val2_Y;

                        if (IsLog)
                        {
                            //float part = (float)ViewRect.Width / 3;
                            float part = (float)(canvas_w / Math.Log10(ViewRect.Width));

                            val1_X = (float)(part * (Math.Log10(m_items[index].X) - Math.Log10(ViewRect.Left)));
                            val1_Y = (m_items[index].Y - ViewRect.Top) * canvas_h / ViewRect.Height; ;
                            val1_Y = canvas_h - val1_Y;

                            val2_X = (float)(part * (Math.Log10(m_items[index].X+1) - Math.Log10(ViewRect.Left)));
                            val2_Y = (m_items[index + 1].Y - ViewRect.Top) * canvas_h / ViewRect.Height;
                            val2_Y = canvas_h - val2_Y;

                        }
                        else
                        {
                            val1_X = (m_items[index].X - ViewRect.Left) * canvas_w / ViewRect.Width;
                            val1_Y = (m_items[index].Y - ViewRect.Top) * canvas_h / ViewRect.Height;
                            val1_Y = canvas_h - val1_Y;

                            val2_X = (m_items[index + 1].X - ViewRect.Left) * canvas_w / ViewRect.Width;
                            val2_Y = (m_items[index + 1].Y - ViewRect.Top) * canvas_h / ViewRect.Height;
                            val2_Y = canvas_h - val2_Y;
                        }

                        points[0] = new PointF((float)(val1_X ), (float)(val1_Y ));
                        points[1] = new PointF((float)(val2_X ), (float)(val2_Y ));

                        g.DrawCurve(new Pen(br), points);

                    }
                    else g.DrawString("Отрисовка приостановлена, так как значение сигнала слишком большое или слишком маленькое", new Font("Times New Roman", 12, FontStyle.Regular, GraphicsUnit.Point), Brushes.Red, new PointF(20, 20));
                }
            }

        }

    }

}
